package com.bjyhl.medical.main.news.cat;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.core.net.RestClient;
import com.bjyhl.core.net.callback.IError;
import com.bjyhl.core.net.callback.IFailure;
import com.bjyhl.core.net.callback.ISuccess;
import com.bjyhl.medical.R;
import com.bjyhl.medical.main.news.NewsFragment;
import com.bjyhl.ui.recycler.MultipleItemEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;

import java.util.List;

/**
 * Created by Natalya on 2019/1/27 18:22
 * Note: 主页fragment
 */

public class NewsCatListFragment extends LatteFragment {

    private RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.fragment_news_cat_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        mRecyclerView = $(R.id.rv_news_cat_list);
        initRecyclerView();
    }

    private void initRecyclerView() {
        final LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(manager);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        RestClient.builder()
                .url("http://192.168.8.101:3000/v1/news/getNewsCatList")
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final List<MultipleItemEntity> data =
                                new NewsCatListDataConverter().setJsonData(response).convert();
                        final NewsFragment fragment = getParentFragments();
                        final NewsCatListRecyclerAdapter adapter = new NewsCatListRecyclerAdapter(data, fragment);
                        mRecyclerView.setAdapter(adapter);
                    }

                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Toast.makeText(getContext(), "加载新闻列表Failure", Toast.LENGTH_LONG).show();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Toast.makeText(getContext(), "加载新闻列表Error" + "("+code+")" + msg, Toast.LENGTH_LONG).show();
                    }
                })
                .build()
                .get();
    }
}
